$(document).ready(function(){
    layui.use(['layer', 'jquery', 'form'], function () {
        var layer = layui.layer,$ = layui.jquery,form = layui.form;

        form.on('select(province)', function(data){
            $params = JSON.stringify({'region_id':data.value});
            $city_back = ajaxSynchroPostRequest("/api/region/get-sub-region",$params);
            if($city_back.success==1){
                $("#city").empty();
                var city_option_html = '<option value="">请选择市</option>';
                $.each($city_back.data,function(index,value){
                    city_option_html += '<option value="'+value.id+'">'+value.name+'</option>';
                });
                $("#city").append(city_option_html);
                form.render('select');
            }
        });
        form.on('select(city)', function(data){
            console.log("选择市 渲染区");
            $params = JSON.stringify({'region_id':data.value});
            $county_back = ajaxSynchroPostRequest("/api/region/get-sub-region",$params);
            if($county_back.success==1){
                $("#county").empty();
                var county_option_html = '<option value="">请选择区</option>';
                $.each($county_back.data,function(index,value){
                    county_option_html += '<option value="'+value.id+'" data-agentuid="'+value.agent_uid+'">'+value.name+'</option>';
                });
                $("#county").append(county_option_html);
                form.render('select');
            }
        });
        
        form.on('select(county)', function(data){
            var agent_uid = $("#county option:selected").attr('data-agentuid') != 0;
            if(agent_uid != 0 && $("#type").val() == 1){//区域代理商申请，检测该区域是否已经有代理商
                layer.msg("该区域已有代理商");
                
                $("#area-remind").html('<div style="color:red;padding-top: 10px;">该区域已有代理商</div>');
                return false;
            }else{
                $("#area-remind").html("");
            }
        });

    });
    
    
layui.use('upload', function(){
    var $ = layui.jquery ,upload = layui.upload;
    //普通图片上传
    var uploadInst = upload.render({
        elem: '#bussiness_img'
        ,url: '/api/upload/img' //改成您自己的上传接口
        ,before: function(obj){
          //预读本地文件示例，不支持ie8
          obj.preview(function(index, file, result){
//              console.log(result);
            $('#demo1').attr('src', result); //图片链接（base64）
          });
        }
        ,done: function(res){
            console.log(res);
            if(res.success==1){
                $('#bussiness_img_preview').html('<img style="width: 100%;height: 120px;" src="'+res.url+'"/>')
                $('#bussiness_img_result').val(res.url);
            }
            
          //如果上传失败
          if(res.code > 0){
            return layer.msg('上传失败');
          }
          //上传成功
        }
        ,error: function(){
          //演示失败状态，并实现重传
          console.log("upload error");
          var demoText = $('#demoText');
          demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</ a>');
          demoText.find('.demo-reload').on('click', function(){
            uploadInst.upload();
          });
        }
  });
  
  var uploadInst = upload.render({
        elem: '#logo_img'
        ,url: '/api/upload/img' //改成您自己的上传接口
        ,before: function(obj){
          //预读本地文件示例，不支持ie8
          obj.preview(function(index, file, result){
//              console.log(result);
            $('#demo1').attr('src', result); //图片链接（base64）
          });
        }
        ,done: function(res){
            console.log(res);
            if(res.success==1){
                $('#logo_img_preview').html('<img style="width: 100%;height: 120px;" src="'+res.url+'"/>');
                $('#logo_img_result').val(res.url);
            }
            
          //如果上传失败
          if(res.code > 0){
            return layer.msg('上传失败');
          }
          //上传成功
        }
        ,error: function(){
          //演示失败状态，并实现重传
          console.log("upload error");
          var demoText = $('#demoText');
          demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</ a>');
          demoText.find('.demo-reload').on('click', function(){
            uploadInst.upload();
          });
        }
  });
  
    $("#apply-submit").on('click',function(){
        var data = {};
        if($("#name").val() == ""){
            layer.msg("请输入公司名称");
            return false;
        }else{
            data['name'] = $("#name").val();
        }

        if($("#province").val() == ""){
            layer.msg("请选择省");
            return false;
        }else{
            data['region_1'] = $("#province").val();
        }

        if($("#city").val() == ""){
            layer.msg("请选择市");
            return false;
        }else{
            data['region_2'] = $("#city").val();
        }

        if($("#county").val() == ""){
            layer.msg("请选择区");
            return false;
        }else{
            data['region_3'] = $("#county").val();
        }

        if($("#address").val() == ""){
            layer.msg("请输入详细地址");
            return false;
        }else{
            data['address'] = $("#address").val();
        }

        if($("#bussiness_no").val() == ""){
            layer.msg("请输入营业执照注册号");
            return false;
        }else{
            data['bussiness_no'] = $("#bussiness_no").val();
        }

        if($("#bussiness_img_result").val() == ""){
            layer.msg("请上传营业执照扫描件");
            return false;
        }else{
            data['bussiness_img'] = $("#bussiness_img_result").val();
        }

        if($("#liaison").val() == ""){
            layer.msg("请输入联系人姓名");
            return false;
        }else{
            data['liaison'] = $("#liaison").val();
        }
        
        data['liaison_id_type'] = $("#liaison_id_type").val();

        if($("#liaison_id_no").val() == ""){
            layer.msg("请输入联系人证件号码");
            return false;
        }else{
            data['liaison_id_no'] = $("#liaison_id_no").val();
        }

        if($("#liaison_job").val() == ""){
            layer.msg("请输入联系人职务");
            return false;
        }else{
            data['liaison_job'] = $("#liaison_job").val();
        }

        if($("#liaison_mobile").val() == ""){
            layer.msg("请输入联系人手机号码");
            return false;
        }else{
            data['liaison_mobile'] = $("#liaison_mobile").val();
        }

        data['liaison_email'] = $("#liaison_email").val();

        if($("#shop_info").val() == ""){
            layer.msg("请输入公司简介");
            return false;
        }else{
            data['shop_info'] = $("#shop_info").val();
        }

        if($("#logo_img_result").val() == ""){
            layer.msg("请上传公司logo");
            return false;
        }else{
            data['shop_logo'] = $("#logo_img_result").val();
        }
        
        data['type'] = $("#type").val();
        
        if(!$('#agree').is(':checked')) {
            layer.msg("请阅读《融媒智投服务协议》");
            return false;
        }
        
        if($("#type").val() == 1){//区域代理商申请，检测该区域是否已经有代理商
            if($("#county option:selected").attr('data-agentuid') != 0){
                layer.msg("该区域已有代理商");
                return false;
            }
        }

        $params = JSON.stringify(data);
        console.log($params);

        $apply_save_back = ajaxSynchroPostRequest("/user/seller/apply-save",$params);
        console.log($apply_save_back);
        //layer.msg($apply_save_back.msg);
        if($apply_save_back.success==1){
            window.location.reload();
        }
      })

    });
    
    
    
    
    
    
});